/**
 * Title : jFamilyTree
 * Description: jFamilyTree
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright:	(c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
  */
package jFamilyTree.commons;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Settings dialog
 * 
 * @author Bernard Bou
 */
public class SettingsDialog extends JDialog
{
	private static final long serialVersionUID = 1L;

	// V A L U E S

	/**
	 * Properties (input/output)
	 */
	protected Properties theSettings;

	/**
	 * Ok result
	 */
	public boolean ok;

	// C O M P O N E N T S

	/**
	 * Base text
	 */
	private JTextField theBaseTextField;

	/**
	 * Repository text
	 */
	private JTextField theImageRepositoryTextField;

	/**
	 * Help text
	 */
	private JTextField theHelpTextField;

	/**
	 * Browser text
	 */
	private JTextField theBrowserTextField;

	/**
	 * Data panel
	 */
	protected JPanel theDataPanel;

	/**
	 * Constructor
	 * 
	 * @param theseSettings
	 *        settings
	 */
	public SettingsDialog(Properties theseSettings)
	{
		super();
		theSettings = theseSettings;
		initialize();
	}

	/**
	 * Initialize
	 * 
	 * @throws Exception
	 */
	protected void initialize()
	{
		setTitle(Messages.getString("SettingsDialog.0")); //$NON-NLS-1$
		setResizable(true);

		// images
		Icon thisIcon = new ImageIcon(SettingsDialog.class.getResource("images/settings.png")); //$NON-NLS-1$
		JLabel thisHeaderLabel = new JLabel();
		thisHeaderLabel.setIcon(thisIcon);
		thisHeaderLabel.setVerticalTextPosition(JLabel.TOP);
		thisHeaderLabel.setHorizontalTextPosition(JLabel.CENTER);
		thisHeaderLabel.setFont(new Font("Dialog", Font.BOLD, 16)); //$NON-NLS-1$
		thisHeaderLabel.setText(Messages.getString("SettingsDialog.1")); //$NON-NLS-1$
		thisHeaderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

		// labels
		JLabel thisWorkFolderLabel = new JLabel(Messages.getString("SettingsDialog.4")); //$NON-NLS-1$
		JLabel thisImageRepositoryFolderLabel = new JLabel(Messages.getString("SettingsDialog.5")); //$NON-NLS-1$
		JLabel thisHelpFolderLabel = new JLabel(Messages.getString("SettingsDialog.6")); //$NON-NLS-1$
		JLabel thisBrowserLabel = new JLabel(Messages.getString("SettingsDialog.7")); //$NON-NLS-1$

		// text
		theBaseTextField = new JTextField(32);
		theImageRepositoryTextField = new JTextField(32);
		theHelpTextField = new JTextField(32);
		theBrowserTextField = new JTextField(32);

		// tooltips
		theBaseTextField.setToolTipText(Messages.getString("SettingsDialog.8")); //$NON-NLS-1$
		theImageRepositoryTextField.setToolTipText(Messages.getString("SettingsDialog.9")); //$NON-NLS-1$
		theHelpTextField.setToolTipText(Messages.getString("SettingsDialog.10")); //$NON-NLS-1$
		theBrowserTextField.setToolTipText(Messages.getString("SettingsDialog.11")); //$NON-NLS-1$

		// buttons
		JButton thisBaseBrowseButton = new JButton(Messages.getString("SettingsDialog.12")); //$NON-NLS-1$
		JButton thisImageRepositoryBrowseButton = new JButton(Messages.getString("SettingsDialog.12")); //$NON-NLS-1$
		JButton thisHelpBrowseButton = new JButton(Messages.getString("SettingsDialog.12")); //$NON-NLS-1$
		JButton thisBrowserBrowseButton = new JButton(Messages.getString("SettingsDialog.12")); //$NON-NLS-1$
		JButton thisOKButton = new JButton(Messages.getString("SettingsDialog.20")); //$NON-NLS-1$
		JButton thisCancelButton = new JButton(Messages.getString("SettingsDialog.21")); //$NON-NLS-1$

		// panels
		theDataPanel = new JPanel();
		theDataPanel.setLayout(new GridBagLayout());
		theDataPanel.add(thisWorkFolderLabel, new GridBagConstraints(0, 0, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 20, 0, 10), 0, 0));
		theDataPanel.add(thisImageRepositoryFolderLabel, new GridBagConstraints(0, 1, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 20, 0, 10), 0, 0));
		// space
		theDataPanel.add(thisHelpFolderLabel, new GridBagConstraints(0, 3, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 20, 0, 10), 0, 0));
		theDataPanel.add(thisBrowserLabel, new GridBagConstraints(0, 4, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 20, 0, 10), 0, 0));

		theDataPanel.add(theBaseTextField, new GridBagConstraints(1, 0, 1, 1, 1., 0., GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
		theDataPanel.add(theImageRepositoryTextField, new GridBagConstraints(1, 1, 1, 1, 1., 0., GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
		// space
		theDataPanel.add(theHelpTextField, new GridBagConstraints(1, 3, 1, 1, 1., 0., GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
		theDataPanel.add(theBrowserTextField, new GridBagConstraints(1, 4, 1, 1, 1., 0., GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));

		theDataPanel.add(thisBaseBrowseButton, new GridBagConstraints(2, 0, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 0, 20), 0, 0));
		theDataPanel.add(thisImageRepositoryBrowseButton, new GridBagConstraints(2, 1, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 0, 20), 0, 0));
		// space
		theDataPanel.add(thisHelpBrowseButton, new GridBagConstraints(2, 3, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 0, 20), 0, 0));
		theDataPanel.add(thisBrowserBrowseButton, new GridBagConstraints(2, 4, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 0, 20), 0, 0));

		JPanel thisButtonPanel = new JPanel();
		thisButtonPanel.setLayout(new FlowLayout());
		thisButtonPanel.add(thisCancelButton);
		thisButtonPanel.add(thisOKButton);

		// action
		thisBaseBrowseButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				String thisFolder = FileDialogs.getFolder(theSettings.getProperty("base", ".")); //$NON-NLS-1$ //$NON-NLS-2$
				if (thisFolder != null && !thisFolder.isEmpty())
					theBaseTextField.setText(thisFolder);
			}
		});
		thisImageRepositoryBrowseButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				String thisFolder = FileDialogs.getFolder(theSettings.getProperty("base", ".")); //$NON-NLS-1$ //$NON-NLS-2$
				if (thisFolder != null && !thisFolder.isEmpty())
					theImageRepositoryTextField.setText(thisFolder);
			}
		});
		thisHelpBrowseButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				String thisFolder = FileDialogs.getFolder(theSettings.getProperty("base", ".")); //$NON-NLS-1$ //$NON-NLS-2$
				if (thisFolder != null && !thisFolder.isEmpty())
					theHelpTextField.setText(thisFolder);
			}
		});
		thisBrowserBrowseButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				String thisFolder = FileDialogs.getExec(theSettings.getProperty("base", ".")); //$NON-NLS-1$ //$NON-NLS-2$
				if (thisFolder != null && !thisFolder.isEmpty())
					theBrowserTextField.setText(thisFolder);
			}
		});
		thisOKButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				ok = true;
				setVisible(false);
			}
		});
		thisCancelButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				setVisible(false);
			}
		});
		
		// assemble
		JPanel thisPanel = new JPanel();
		thisPanel.setLayout(new BoxLayout(thisPanel, BoxLayout.Y_AXIS));
		thisPanel.add(Box.createRigidArea(new Dimension(0, 10)));
		thisPanel.add(thisHeaderLabel);
		thisPanel.add(theDataPanel);
		thisPanel.add(thisButtonPanel);
		thisPanel.add(Box.createRigidArea(new Dimension(0, 10)));

		setContentPane(thisPanel);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setVisible(boolean)
	 */
	public void setVisible(boolean thisFlag)
	{
		if (thisFlag)
		{
			ok = false;

			// read properties into components
			theBaseTextField.setText(theSettings.getProperty("base")); //$NON-NLS-1$
			theImageRepositoryTextField.setText(theSettings.getProperty("images")); //$NON-NLS-1$
			theHelpTextField.setText(theSettings.getProperty(Messages.getString("SettingsDialog.28"))); //$NON-NLS-1$
			theBrowserTextField.setText(theSettings.getProperty(Messages.getString("SettingsDialog.29"))); //$NON-NLS-1$

			pack();
			Utils.center(this);
		}
		else
		{
			if (ok)
			{
				// update properties from components
				theSettings.setProperty(Messages.getString("SettingsDialog.30"), theBaseTextField.getText()); //$NON-NLS-1$
				theSettings.setProperty(Messages.getString("SettingsDialog.31"), theImageRepositoryTextField.getText()); //$NON-NLS-1$
				theSettings.setProperty(Messages.getString("SettingsDialog.32"), theHelpTextField.getText()); //$NON-NLS-1$
				theSettings.setProperty(Messages.getString("SettingsDialog.33"), theBrowserTextField.getText()); //$NON-NLS-1$
			}
		}
		super.setVisible(thisFlag);
	}
}
